<template>
  <el-card>
    <span slot="header">商品添加</span>
    <el-form ref="form" :model="form" label-width="80px" style="width: 484px">
      <!-- 商品名称 -->
      <el-form-item label="商品名称">
        <el-input v-model="form.name" placeholder="商品名称"></el-input>
      </el-form-item>
      <!-- 商品分类 -->
      <el-form-item label="商品分类">
        <el-select v-model="form.category" placeholder="请选择商品分类">
          <!-- <el-option label="分类一" value="分类一"></el-option>
          <el-option label="分类二" value="分类二"></el-option> -->
          <el-option :value="item" v-for="item in categories" :key="item.id"
            :label="item"></el-option>
        </el-select>
      </el-form-item>
      <!-- 商品价格 -->
      <el-form-item label="商品价格">
        <el-input-number v-model="form.price" @change="handleChange" :min="1"
          :max="10" label="描述文字"></el-input-number>
      </el-form-item>
      <!-- 商品图片 -->
      <el-form-item label="商品图片">
        <el-upload :action="`${API}/goods/goods_img_upload`"
          list-type="picture-card" :on-success="handleAvatarSuccess"
          :on-preview="handlePictureCardPreview" :on-remove="handleRemove">
          <i class="el-icon-plus"></i>
        </el-upload>
        <el-dialog :visible.sync="dialogVisible">
          <img width="100%" :src="dialogImageUrl" alt="" />
        </el-dialog>
      </el-form-item>
      <!-- 商品描述 -->
      <el-form-item label="商品描述">
        <el-input type="textarea" v-model="form.goodsDesc"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">添加商品</el-button>
      </el-form-item>
    </el-form>
  </el-card>
</template>

<script>
import { addGoodsReq, checkCateGoriesReq } from "@/api/goods";

export default {
  data() {
    return {
      API: "http://sell.h5.itsource.cn:8087",
      form: {
        name: "",
        category: "",
        price: 1,
        imgUrl: "",
        goodsDesc: "",
      },
      //商品分类
      categories: [],
      //图片
      dialogImageUrl: "",
      dialogVisible: false,
    };
  },
  methods: {
    //添加
    async onSubmit() {
      console.log(this.form);
      let res = await addGoodsReq(this.form);
      console.log("res.data :>> ", res.data);
      let { code, msg } = res.data;
      if (code === 0) {
        this.$router.push("/goods/goods-list");
      }
    },

    //获取分类
   async getCate() {
      let res = await checkCateGoriesReq();
      console.log('res.data :>> ', res.data);
      this.categories=res.data
    },
    // 计数
    handleChange(value) {
      console.log(value);
    },
    //图片
    handleRemove(file, fileList) {
      console.log(file, fileList);
    },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url;
      this.dialogVisible = true;
    },
    //图片上传成功
    handleAvatarSuccess(res) {
      console.log(res);
      let { code, imgUrl } = res;
      this.form.imgUrl = imgUrl;
    },
  },
  created(){
    this.getCate()
  }
};
</script>

<style lang="scss" scoped>
.avatar-uploader :deep(.el-upload) {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 90px;
  height: 90px;
  line-height: 90px;
  text-align: center;
}
.avatar {
  width: 90px;
  height: 90px;
  display: block;
}
</style>
